package org.zjx.app.action.window;

import org.zjx.analysis.analyze.game.GameClickAnalysis;
import org.zjx.command.entity.GameMouse;
import org.zjx.command.entity.screen.Address;
import org.zjx.command.entity.screen.TabMap;
import org.zjx.window.action.Action;
import org.zjx.window.window.IWindow;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

public class ClickSceneAction extends Action {
    private GameClickAnalysis gameClickAnalysis;
    private final List<Address> clickAddressList = new ArrayList<>();
    private final ClickType clickType;

    public ClickSceneAction(IWindow targetWindow, Address clickAddress, ClickType clickType) {
        super(targetWindow);
        this.clickAddressList.add(clickAddress);
        this.clickType = clickType;
    }

    public ClickSceneAction(IWindow targetWindow, List<Address> clickAddressList, ClickType clickType) {
        super(targetWindow);
        this.clickAddressList.addAll(clickAddressList);
        this.clickType = clickType;
    }

    private void analysis(Address address) {
        gameClickAnalysis = new GameClickAnalysis(targetWindow, address);
    }

    @Override
    protected void doExecute() throws Exception {
        for (Address clickAddress : clickAddressList) {
            analysis(clickAddress);
            Point point = gameClickAnalysis.getSafeClickPoint();
            if (clickType == ClickType.NPC) {
                targetWindow.actions().gameMoveTo(point.x, point.y - 20).delay(50);
                targetWindow.flushScreenshot();
                if (targetWindow.getState().getGameState().getGameMouse().type == GameMouse.Type.NPC) {
                    targetWindow.actions().click(1).delay(1000);
                }
            } else if (clickType == ClickType.GIVE) {
                targetWindow.actions().gameMoveTo(point.x, point.y - 20).delay(50);
                targetWindow.flushScreenshot();
                if (targetWindow.getState().getGameState().getGameMouse().type == GameMouse.Type.NPC) {
                    targetWindow.actions().hotkey(KeyEvent.VK_ALT, KeyEvent.VK_G).delay(100).click(1).delay(1000);
                }
            } else {
                targetWindow.actions().gameMoveTo(point.x, point.y).delay(50);
                targetWindow.actions().click(1).delay(1000);
            }
        }

    }

    public enum ClickType {
        NPC,
        GIVE,
        MONSTER,
        SCENE
    }
}
